#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    imgInit();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::imgInit()
{
    Mat imgData = imread("test01.jpg");
    Mat gray;
    cvtColor(imgData,imgData,COLOR_BGR2RGB);
    myImg = imgData;
    myQImg = QImage((const uchar*)(myImg.data),myImg.cols,myImg.rows,QImage::Format_RGB888);
    imgShow();
}

void MainWindow::imgProc(float alpha)
{
    Mat src1 = myImg;
    Mat src2 = imread("test02.jpg");
    cvtColor(src2,src2,COLOR_BGR2RGB);
    //Warning 因为我们对 src1 和 src2 求 和 ，它们必须要有相同的尺寸（宽度和高度）和类型。
    cv::resize(src2,src2,Size(src1.cols,src1.rows));

    Mat dst;
    cv::addWeighted(src2,alpha,src1,1-alpha,0,dst);  //g(i,j)=alpha*f0(i,j)+(1-alpha)*f1(i,j)
    myQImg = QImage((const uchar*)(dst.data),dst.cols,dst.rows,QImage::Format_RGB888);
    imgShow();
}

void MainWindow::imgShow()
{
    ui->label->setPixmap(QPixmap::fromImage(myQImg.scaled(ui->label->size())));
}


void MainWindow::on_verticalSlider_sliderMoved(int position)
{
    imgProc(position/100.0);
}


void MainWindow::on_verticalSlider_valueChanged(int value)
{
    imgProc(value/100.0);
}

